﻿using Hotel.Application.Admins.Dto;
using Hotel.Application;
using Hotel.Domain;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using SqlSugar;
using Hotel.Web.Filters;

namespace Hotel.Web.Controllers
{
    //[NonController]
    public abstract class BaseController<TEntity, TCreateUpdateDto, TListDto> : Controller
        where TEntity : class, new()
        where TListDto : class, new()
        where TCreateUpdateDto : class, new()
    {
        private readonly IBaseService<TEntity, TCreateUpdateDto, TListDto> service;

        public BaseController(IBaseService<TEntity, TCreateUpdateDto, TListDto> service)
        {
            this.service = service;
        }

        #region 默认列表
        /// <summary>
        /// 默认列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual IActionResult Index()
        {
            return View();
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<IActionResult> Create()
        {
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<IActionResult> Create(TCreateUpdateDto dto)
        {
            return Json(await service.InsertAsync(dto));
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<IActionResult> Edit(long id)
        {
            return View(await service.FindAsync(id));
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, UnitOfWork]
        public virtual async Task<IActionResult> Edit(TCreateUpdateDto dto)
        {
            return Json(await service.UpdateAsyc(dto));
        }
        #endregion

        #region 删除        
        /// <summary>
        /// 单条删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<IActionResult> Delete(long id)
        {
            return Json(await service.DeleteAsync(id));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, UnitOfWork]
        public virtual async Task<IActionResult> Delete(List<long> ids)
        {
            return Json(await service.ManyDeleteAsync(ids));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete, UnitOfWork]
        public virtual async Task<IActionResult> Delete(List<TEntity> entities)
        {
            return Json(await service.ManyDeleteAsync(entities));
        }
        #endregion

        #region 查询、分页
        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<IActionResult> Query()
        {
            return Json(await service.QueryAsync());
        }

        /// <summary>
        /// 异步分页
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public virtual async Task<IActionResult> PageAsync(Expressionable<TEntity> expressionable, int PageSize = 10, int PageIndex = 1)
        {
            var pageData = await service.ToPageListAsync(expressionable, PageSize, PageIndex);
            return Json(pageData);
        }

        /// <summary>
        /// 异步分页
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public virtual IActionResult PageSync(Expressionable<TEntity> expressionable, int PageSize = 10, int PageIndex = 1)
        {
            var pageData = service.ToPageList(expressionable, PageSize, PageIndex);
            return Json(pageData);
        }
        #endregion        
    }
}
